package com.jasmine.bjstkserver.server.article.online;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.jasmine.auth.util.AuthUtil;
import com.jasmine.bjstkserver.core.config.annotation.AuthIgnoreLimiter;
import com.jasmine.bjstkserver.server.article.online.model.ValidArtOnline;
import com.jasmine.core.constants.enums.ParamEnum;
import com.jasmine.bjstkserver.server.article.online.model.ArticleReq;
import com.jasmine.bjstkserver.server.article.log.viewlog.ViewLog;
import com.jasmine.bjstkserver.server.param.ParamEntity;
import com.jasmine.bjstkserver.server.param.ParamService;
import com.jasmine.core.dto.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 文章发布
 *
 * @author jasmineXz
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ParamService paramService;


    /**
     * 查询发布文章列表
     * @param article 查询条件
     * @return 文章列表
     */
    @AuthIgnoreLimiter
    @GetMapping("/list")
    public R findList (@ModelAttribute @Validated(ValidArtOnline.List.class) ArticleReq article) {
        article.setUserId(Integer.valueOf(paramService.findOne(ParamEnum.BLOG_USER_ID.id()).getParamValue()));
        return R.ok(articleService.findList(article));
    }


    /**
     * 查询文章内容
     * @param article 查询条件
     * @return 文章内容
     */
    @GetMapping
    @AuthIgnoreLimiter
    @ViewLog(type = {"CLICK","VIEW"})
    public R findOne (@ModelAttribute @Validated(ValidArtOnline.One.class) ArticleReq article) {
        article.setUserId(Integer.valueOf(paramService.findOne(ParamEnum.BLOG_USER_ID.id()).getParamValue()));
        return R.ok(articleService.findOne(article));
    }


    /**
     * 发布文章
     * @param article 文章ID
     */
    @PostMapping
    public R publish (@RequestBody ArticleReq article) throws Exception {
        article.setUserId(AuthUtil.getUserId());
        return R.ok(articleService.publish(article));
    }


    /**
     * 撤销文章
     * @param article 文章ID
     */
    @DeleteMapping
    public R abolish (@RequestBody ArticleReq article) {
        article.setUserId(AuthUtil.getUserId());
        return R.ok(articleService.abolish(article));
    }


    /**
     * 获取二维码
     * @param articleId 文章ID
     */
    @GetMapping("/qrcode")
    public void getQRCode(@RequestParam("articleId")Integer articleId, HttpServletResponse response) throws IOException {
        String domain = paramService.findOne(ParamEnum.BL0G_H5_URL.id()).getParamValue();
        BufferedImage bfi = QrCodeUtil.generate(
                domain + articleId,
                new QrConfig(200,200));
        response.setContentType("image/png");
        response.addHeader("Article-Url",domain + articleId);
        ImageIO.write(bfi,"png",response.getOutputStream());
        response.getOutputStream().close();
    }


    /**
     * 获取WEB文章路径
     * @param articleId 文章ID
     */
    @GetMapping("/weburl")
    public R getWebUrl(@RequestParam("articleId") Integer articleId) {
        String domain = paramService.findOne(ParamEnum.BL0G_WEB_URL.id()).getParamValue();
        return R.ok(domain + articleId);
    }




    /* ------------------- 统计 ------------------- */




    /**
     * 文章树状统计
     * @param type 类型 [art:=包含文章名;folder或空:只有文件夹]
     * @return 文章文件夹树状列表
     */
    @AuthIgnoreLimiter
    @GetMapping("/statis/tree")
    public R findStatisMind (@RequestParam(value = "type",required = false)String type) {
        ParamEntity param = paramService.findOne(ParamEnum.BLOG_USER_ID.id());
        ArticleReq article = new ArticleReq();
        article.setUserId(Integer.valueOf(param.getParamValue()));
        return R.ok(articleService.findStatisTree(article,type));
    }


    /**
     * 文章统计
     * @return 文章数,字数,点击数,阅读数
     */
    @AuthIgnoreLimiter
    @GetMapping("/statis/num")
    public R findStatisNum () {
        ParamEntity param = paramService.findOne(ParamEnum.BLOG_USER_ID.id());
        ArticleReq article = new ArticleReq();
        article.setUserId(Integer.valueOf(param.getParamValue()));
        return R.ok(articleService.findStatisNum(article));
    }


}
